package com.study.bean;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 切面类：通知+切入点
 */
@Component//该注解不能省略
@Aspect //标记该类是一个切面类
public class MyAspect {

    @Before("MyAspect.teacherServicePointcut()")
    public void before(Joinpoint joinpoint) {
        System.out.println("注解前置通知");
    }

    @AfterReturning("MyAspect.teacherServicePointcut()")
    public void afterReturning() {
        System.out.println("注解后置通知");
    }

    @After("MyAspect.teacherServicePointcut()")
    public void after() {
        System.out.println("注解最终通知");
    }

    @AfterThrowing("MyAspect.teacherServicePointcut()")
    public void afterThrowing() {
        System.out.println("注解异常通知");
    }

    @Around("MyAspect.teacherServicePointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //定义返回值
        Object obj = null;
        try {
            //获取方法执行所需要的参数
            Object[] args = joinPoint.getArgs();
            System.out.println("注解环绕通知的前置通知");
            obj = joinPoint.proceed(joinPoint.getArgs());
            System.out.println("注解环绕通知的后置通知");
        } catch (Exception ex) {
            System.out.println("注解环绕通知的异常通知");
        } finally {
            System.out.println("注解环绕通知的最终通知");
        }
        return obj;
    }

    @Pointcut(value = "execution(* com.study.bean.TeacherServiceImpl.save(*))")
    public void teacherServicePointcut() {
    }

}
